1686D - Linguistics - CodeForces Solution


constructive algorithms greedy sortings strings two pointers *2000

Please click on ads to support us..

C++ Code:

#include <string>
#include <iomanip>
#include <iostream>
#include <vector>
#include <set>
#include <bitset>
#include <map>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <numeric>
#include <cstring>
#include <complex>
#include <cassert>
#include <functional>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef vector<pii> vii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef complex<double> cd;
 
#define x first
#define y second
#define mp make_pair 
#define pb push_back
#define eb emplace_back
#define all(v) v.begin(), v.end()

const int INF = 1e9 + 1;
const ll INF_LL = (ll)1e18;
// const ll MOD = 1e9 + 7;
const int MOD = 998244353;
const int ALPHA = 26;
const int MAXN = 2e5 + 200;
const int LOG = 18;
const int SIEVE = (int)1e6 + 10;
const ld EPS = 1e-9;
const ld SCALE = 1e+6;

void solve() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    string s; cin >> s;
    int n = (int) s.size();
    int as = a + c + d;
    int cnt = 0;
    for (char ch : s)
        cnt += (ch == 'A');

    if (as != cnt) {
        cout << "NO" << '\n';
        return;
    }

    int any = 0;
    int total = 0;
    vi cs, ds;
    int beg = 0;
    while (beg < n) {
        int end = beg;
        while (end + 1 < n && s[end + 1] != s[end]) {
            end += 1;
        }
        int cc = end - beg;
        if (cc > 0) {
            total += cc / 2;
            if (cc % 2 == 0) {
                any += cc / 2;
            } else {
                if (s[beg] == 'A') {
                    cs.push_back(cc / 2);
                } else {
                    ds.push_back(cc / 2);
                }
            }
        }
        beg = end + 1;
    }

    sort(all(cs)), sort(all(ds));
    int rem_c = c;
    int rem_d = d;
    for (int x : cs) {
        if (rem_c >= x + 1) {
            rem_c -= x + 1;
            total += 1;
        }
    }
    for (int x : ds) {
        if (rem_d >= x + 1) {
            rem_d -= x + 1;
            total += 1;
        }
    }
    cout << (total >= c + d ? "YES" : "NO") << '\n';
}    

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}  


Comments

Submit
0 Comments
More Questions

703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal
1505C - Fibonacci Words
1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String